import { Context } from 'koa';
import tagService from '../services/tag.service';

/**
 * 获取标签列表
 * @param ctx Koa上下文
 */
export const getTags = async (ctx: Context) => {
  try {
    const { 
      keyword, 
      sort_by, 
      sort_order, 
      page, 
      limit,
      status
    } = ctx.query;

    const params = {
      keyword: keyword as string,
      sort_by: sort_by as string,
      sort_order: sort_order as 'ASC' | 'DESC',
      page: page ? parseInt(page as string) : undefined,
      limit: limit ? parseInt(limit as string) : undefined,
      status: status as 'active' | 'inactive',
    };

    const result = await tagService.getTags(params);
    
    ctx.body = {
      code: 200,
      data: result,
      message: '获取标签列表成功',
    };
  } catch (error: any) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: `获取标签列表失败: ${error.message}`,
    };
  }
};

/**
 * 获取热门标签
 * @param ctx Koa上下文
 */
export const getHotTags = async (ctx: Context) => {
  try {
    const { limit } = ctx.query;
    
    const tags = await tagService.getHotTags(limit ? parseInt(limit as string) : undefined);
    
    ctx.body = {
      code: 200,
      data: tags,
      message: '获取热门标签成功',
    };
  } catch (error: any) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: `获取热门标签失败: ${error.message}`,
    };
  }
};

/**
 * 获取标签详情
 * @param ctx Koa上下文
 */
export const getTagById = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    
    const tag = await tagService.getTagById(id);
    
    ctx.body = {
      code: 200,
      data: tag,
      message: '获取标签详情成功',
    };
  } catch (error: any) {
    ctx.status = 404;
    ctx.body = {
      code: 404,
      message: `获取标签详情失败: ${error.message}`,
    };
  }
};

/**
 * 根据Slug获取标签详情
 * @param ctx Koa上下文
 */
export const getTagBySlug = async (ctx: Context) => {
  try {
    const { slug } = ctx.params;
    
    const tag = await tagService.getTagBySlug(slug);
    
    ctx.body = {
      code: 200,
      data: tag,
      message: '获取标签详情成功',
    };
  } catch (error: any) {
    ctx.status = 404;
    ctx.body = {
      code: 404,
      message: `获取标签详情失败: ${error.message}`,
    };
  }
};

/**
 * 创建标签
 * @param ctx Koa上下文
 */
export const createTag = async (ctx: Context) => {
  try {
    const {
      name,
      slug,
      description,
      status,
    } = ctx.request.body;
    
    // 验证必填字段
    if (!name || !slug) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '标签名称和别名不能为空',
      };
      return;
    }
    
    const tag = await tagService.createTag({
      name,
      slug,
      description,
      status,
    });
    
    ctx.status = 201;
    ctx.body = {
      code: 201,
      data: tag,
      message: '创建标签成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `创建标签失败: ${error.message}`,
    };
  }
};

/**
 * 更新标签
 * @param ctx Koa上下文
 */
export const updateTag = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    const {
      name,
      slug,
      description,
      status,
    } = ctx.request.body;
    
    const tag = await tagService.updateTag(id, {
      name,
      slug,
      description,
      status,
    });
    
    ctx.body = {
      code: 200,
      data: tag,
      message: '更新标签成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `更新标签失败: ${error.message}`,
    };
  }
};

/**
 * 删除标签
 * @param ctx Koa上下文
 */
export const deleteTag = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    
    const result = await tagService.deleteTag(id);
    
    ctx.body = {
      code: 200,
      data: result,
      message: '删除标签成功',
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `删除标签失败: ${error.message}`,
    };
  }
};

/**
 * 批量删除标签
 * @param ctx Koa上下文
 */
export const deleteTagsBatch = async (ctx: Context) => {
  try {
    const { ids, force } = ctx.request.body;
    
    if (!Array.isArray(ids) || ids.length === 0) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '请选择要删除的标签',
      };
      return;
    }
    
    const result = await tagService.deleteTagsBatch(ids, !!force);
    
    ctx.body = {
      code: 200,
      data: result,
      message: `成功删除${ids.length}个标签`,
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `批量删除标签失败: ${error.message}`,
    };
  }
};

/**
 * 合并标签
 * @param ctx Koa上下文
 */
export const mergeTags = async (ctx: Context) => {
  try {
    const { source_ids, target_id } = ctx.request.body;
    
    if (!Array.isArray(source_ids) || source_ids.length === 0) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '请选择要合并的源标签',
      };
      return;
    }
    
    if (!target_id) {
      ctx.status = 400;
      ctx.body = {
        code: 400,
        message: '请选择目标标签',
      };
      return;
    }
    
    const result = await tagService.mergeTags(source_ids, target_id);
    
    ctx.body = {
      code: 200,
      data: result,
      message: result.message,
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `合并标签失败: ${error.message}`,
    };
  }
};

/**
 * 同步标签文章数量
 * @param ctx Koa上下文
 */
export const syncTagArticleCount = async (ctx: Context) => {
  try {
    const { id } = ctx.params;
    
    const result = await tagService.syncTagArticleCount(id);
    
    ctx.body = {
      code: 200,
      data: result,
      message: result.message,
    };
  } catch (error: any) {
    ctx.status = 400;
    ctx.body = {
      code: 400,
      message: `同步标签文章数量失败: ${error.message}`,
    };
  }
}; 